<?php
require_once 'login.php';
require_once 'DAO/DAORepublica.php';
require_once 'DAO/DAOUsuarioRepublica.php';
require_once 'DAO/DAOSolicitacoes.php';
require_once 'DAO/DAOEstatisticaRepublica.php';
require_once 'DAO/DAOComentarioRepublica.php';
require_once 'DAO/DAOImagemRepublica.php';

class RepublicaController {

    private $republica;
    private $republicaUsuario;

    function RepublicaController() {
        $this->republicaUsuario = new Republica();
        $gets = explode("/", $_SERVER['REQUEST_URI']);
        $codRepublica = (isset($gets[2])) ? $gets[2] : NULL;
        if (LoginController::singleton()->estaConectado()) {
            $this->usuario = LoginController::singleton()->getUsuario();
            if (LoginController::singleton()->getTipo() == TIPO_USUARIO) {
                $this->republicaUsuario = LoginController::singleton()->getUsuario()->getRepublicaAtual();
            }
        }
        if (is_numeric($codRepublica)) {
            $dr = new DAORepublica();
            $this->republica = $dr->getRepublica($codRepublica);
            if (LoginController::singleton()->estaConectado()) {
                $this->addEstatistica();
                $this->verificaFormSolicitacaoRepublica();
                $this->verificaFormComentario();
                $this->verificaFormDeletarComentarioRepublica();
            }

            require_once 'viewRepublica.php';
        }
    }

    // Verifica se tem comentario para apagar
    function verificaFormDeletarComentarioRepublica() {
        if (isset($_POST['formDeleteComentarioRepublica'])) {
            if (is_numeric($_POST['edtComentario'])) {
                $dcr = new DaoComentarioRepublica();
                $dcr->deleteComentarioById($_POST['edtComentario']);
            }
        }
    }

    function addEstatistica() {
        /*          Add contagem as Estatistica do evento           */
        $DAOEstatisticaRepublica = new DAOEstatisticaRepublica();
        $DAOEstatisticaRepublica->addEstatisticaRepublica(new Estatistica($this->republica, LoginController::singleton()->getUsuario(), 0, 0, $_SERVER["REMOTE_ADDR"]));
    }

    function verificaFormSolicitacaoRepublica() {
        /*          Verifica Solicitações                           */
        $ds = new DAOSolicitacoes();
        $solicitacao = new Solicitacoes();
        $solicitacao->setRepublica($this->republica);
        $solicitacao->setUsuario(LoginController::singleton()->getUsuario());
        if (isset($_POST['formSolicitacaoRepublica'])) {
            $ds->addSolicitacao($solicitacao);
        }
        if (isset($_POST['formRemoveSolicitacaoRepublica'])) {
            $ds->removeSolicitacao($solicitacao);
        }
    }

    function verificaFormComentario() {
        /*          Verifica se foi Enviado um Comentario           */
        if (isset($_POST['formComentarioRepublica'])) {
            $DAOComentario = new DAOComentarioRepublica();
            $DAOComentario->addComentario(new Comentario(LoginController::singleton()->getUsuario(), $this->republica, $_POST['edtComentario']));
        }
    }

    function getMoradores(Republica $republica) {

        $usuariosRepublica = $republica->getMoradores();

        if ($usuariosRepublica != NULL) {
            ?>
            <table class="table table-striped">
                <?
                foreach ($usuariosRepublica as $usu) {
                    ?>
                    <tr>
                        <td><img src="/img/icon-Empresa.png" style="max-height: 20px; min-height: 20px;"/></td>
                        <td><? echo $usu->getUsuario()->getNome(); ?></td>
                    </tr>
                    <?
                }
                ?>

            </table>
            <?
        }
    }

    function getFormSolicitacao($republica) {
        if (isset($_SESSION['Usuario'])) {
            if ($_SESSION['USU_TIPO'] == TIPO_USUARIO) {
                $usuario = $_SESSION['Usuario'];

                $rep = $usuario->getRepublicaAtual();
                if ($rep == NULL) {
                    $ds = new DAOSolicitacoes();
                    $solicitacao = new Solicitacoes();
                    $solicitacao->setRepublica($republica);
                    $solicitacao->setUsuario($usuario);

                    if ($ds->usuarioSolicitou($solicitacao)) {
                        ?>
                        <form method="POST" action="">
                            <input type="hidden" id="formRemoveSolicitacaoRepublica" name="formRemoveSolicitacaoRepublica" value="formRemoveSolicitacaoRepublica"/>
                            <button type="submit" class="btn">Retirar Solicitação</button>
                        </form>
                        <?
                    } else {
                        ?>
                        <form method="POST" action="">
                            <input type="hidden" id="formSolicitacaoRepublica" name="formSolicitacaoRepublica" value="formSolicitacaoRepublica"/>
                            <button type="submit" class="btn">Inscreva-se para esta republica</button>
                        </form>
                        <?
                    }
                }
            }
        }
    }

    function getNotaRepublica(Republica $republica) {
        ?>
        <div class="span4 text-center label label-success">
            <i class="icon-star"> </i><i class="icon-star"> </i><i class="icon-star"> </i><i class="icon-star"> </i><i class="icon-star"> </i>
            <h1 style="margin-top: 0px;">Nota: 10.0</h1>            
        </div>
        <?
    }

    function getComentariosRepublica(Republica $republica, $limite = 0) {

        $comentarios = $republica->getComentarios($limite);
        ?>
        <div class="span8">
            <?
            foreach ($comentarios as $comentario) {
                ?>
                <blockquote>
                    <p style="word-wrap: break-word;"><? echo $comentario->getComentario(); ?></p>
                    <small style="word-wrap: break-word;"><? echo $comentario->getUsuario()->getNome(); ?></small>

                    <?
                    if (LoginController::singleton()->estaConectado()) {
                        if (LoginController::singleton()->getUsuario()->getCodigo() == $comentario->getUsuario()->getCodigo()) {
                            ?>
                            <form action = "" method = "POST">
                                <input type = "hidden" name = "formDeleteComentarioRepublica" value = "formDeleteComentarioRepublica"/>
                                <input type = "hidden" name = "edtComentario" value = "<? echo $comentario->getCodigo(); ?>"/>
                                <right><button class="btn" name="btnSubmit" type="submit" value="delete" title="Excluir"><i class="icon-remove"></i></button></right>
                            </form>
                            <?
                        } else {
                            if ($this->republica->getCodigo() == $this->republicaUsuario->getCodigo()) {
                                ?>
                                <form action = "" method = "POST">
                                    <input type = "hidden" name = "formDeleteComentarioRepublica" value = "formDeleteComentarioRepublica"/>
                                    <input type = "hidden" name = "edtComentario" value = "<? echo $comentario->getCodigo(); ?>"/>
                                    <right><button class="btn" name="btnSubmit" type="submit" value="delete" title="Excluir"><i class="icon-remove"></i></button></right>
                                </form>
                                <?
                            }
                        }
                    }
                    ?> </blockquote> <?
            }
            if (isset($_SESSION['Usuario'])) {
                ?>
                <form method="POST" action="">
                    <input type="hidden" name="formComentarioRepublica" id="formComentarioRepublica"/>
                    <textarea class="span12" name="edtComentario" id="edtComentario" placeholder="Diga o que você acha desta republica..."></textarea>
                    <button type="submit" class="btn">Enviar Comentario</button>
                </form>
                <?
            }
            ?>
        </div>
        <?
    }

    function getTodosComentarios() {
        ?>
        <div class="span12" style="padding: 0px; margin: 0px;">
            <center>
                <a href="#modalComentario" role="button" class="btn" data-toggle="modal">Ver Todos os Comentarios</a>
            </center>
            <div id="modalComentario" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Comentarios</h3>
                </div>
                <div class="modal-body well">
                    <? $this->getComentariosRepublica($this->getRepublica()); ?>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Fechar</button>
                </div>

            </div>
        </div>
        <?
    }

    function getComentariosEventosRepublica(Republica $republica) {

        $comentarios = $republica->getComentarios(3);
        ?>
        <div class="span8">
            <?
            foreach ($comentarios as $comentario) {
                ?>
                <blockquote>
                    <p><? echo $comentario->getComentario(); ?></p>
                    <small><? echo $comentario->getUsuario()->getNome(); ?></small>
                </blockquote>
                <?
            }
            ?>
            <?
            if (isset($_SESSION['Usuario'])) {
                ?>
                <form method="POST" action="">
                    <input type="hidden" name="formComentarioRepublica" id="formComentarioRepublica"/>
                    <textarea class="span12" name="edtComentario" id="edtComentario" placeholder="Diga o que você acha desta republica..."></textarea>
                    <button type="submit" class="btn">Enviar Comentario</button>
                </form>
                <?
            }
            ?>
        </div>
        <?
    }

    function getImagensRepublica(Republica $republica) {
        ?>
        <div id="principal" class="lightbox hide fade"  tabindex="-1" role="dialog" aria-hidden="true">
            <div class='lightbox-content'>
                <img class="img-polaroid span12" style="margin: 0px;" src="/img/<? echo $republica->getImagemPrincipal()->getImagem() ?>" alt="photo">';
            </div>
        </div>
        <a data-toggle="lightbox" href="#principal" data-target="#principal"><img class="img-polaroid span12" style="margin: 0px;" src="/img/<? echo $republica->getImagemPrincipal()->getImagem() ?>" alt="photo"></a>

        <br>
        <?
        ?>
        <div class="row-fluid">
            <?
            $image = $republica->getImagens(4);
            $cont = 0;
            foreach ($image as $img) {
                ?>
                <div id="principal<? echo $cont; ?>" class="lightbox hide fade"  tabindex="-1" role="dialog" aria-hidden="true">
                    <div class='lightbox-content'>
                        <img class="img-polaroid span12" src="/img/<? echo $img->getImagem(); ?>" alt="photo">';
                    </div>
                </div>
                <a data-toggle="lightbox" href="#principal<? echo $cont; ?>" data-target="#principal<? echo $cont; ?>"><img class="img-polaroid span3" src="/img/<? echo $img->getImagem(); ?>" style="max-height: 50px; min-height: 50px;" alt="photo"></a>
                    <?
                    $cont = $cont + 1;
                }
                ?>
        </div>
        <?
    }

    public function getRepublica() {
        return $this->republica;
    }

}
?>
